package com.tender.impl;

import com.tender.LoginBizService;
import com.tender.dto.LoginDTO;
import com.tender.utils.LoginUser;
import com.tender.utils.SnowFlakeUtil;
import com.tender.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginBizServiceImpl implements LoginBizService {

    @Autowired
    private TokenService tokenService;

    @Override
    public String submit(LoginDTO param) {
        LoginUser loginUser = new LoginUser();
        if (null == param.getUserId()) {
            param.setUserId(Long.parseLong(SnowFlakeUtil.nextId()));
        }
        loginUser.setUserId(param.getUserId());
        loginUser.setUsername(param.getUsername());

        return tokenService.createToken(loginUser);
    }

    @Override
    public LoginDTO parse() {
        LoginUser loginUser = tokenService.parse();
        if (null != loginUser) {
            LoginDTO dto = new LoginDTO();
            dto.setUsername(loginUser.getUsername());
            dto.setUserId(loginUser.getUserId());

            return dto;
        }

        return null;
    }
}
